package com.hxzy.controller;

import cn.hutool.core.util.IdUtil;
import com.hxzy.common.constans.CaptchaConst;
import com.hxzy.common.constans.RedisConstans;
import com.hxzy.common.vo.R;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author Administrator
 */
@Api(tags = "验证码API")
@RequestMapping(value = "/common")
@RestController
public class CaptchaController {

    /**
     * 图片长度
     */
    @Value(value = "${catpcha.len:2}")
    private Integer len;

    /**
     * 验证码类型(png 英文, math 数字验证)
     */
    @Value(value = "${catpcha.type:math}")
    private String type;

    /**
     * spring框架帮我们封装redis操作模板类
     */
    @Autowired
    private RedisTemplate  redisTemplate;


    /**
     * @RequestParam  = request.getParameter("参数")
     * @RequestParam(value = "width",defaultValue = "150")
     * int width=0;
     * if(request.getParameter("width")==null){
     *     width=150;
     * }else{
     *     width=Integer.parseInt(request.getParameter("width"));
     * }
     * 生成验证码
     * @param width 图片宽
     * @param height 图片高
     * @return
     */
    @GetMapping(value = "/captcha")
    @ApiOperation(value = "生成验证码")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "width",value = "图片宽度"),
            @ApiImplicitParam(name = "height",value = "图片高度")
    })
    public R  captcha(@RequestParam(value = "width",defaultValue = "150") int width,
                      @RequestParam(value = "height",defaultValue = "50") int height ){

        // 简单工厂设计模式
        Captcha  captcha=null;
        //算术验证码
        if(CaptchaConst.MATH.equalsIgnoreCase(this.type)){
            captcha=new ArithmeticCaptcha(width,height);
            if(this.len>CaptchaConst.MATH_MAX_LEN){
                this.len=CaptchaConst.MATH_MAX_LEN;
            }
        }else {
            captcha=new SpecCaptcha(width,height);
        }
        captcha.setLen(len);

        //输出结果
        String value= captcha.text();
        //把图片转换为base64的字符串格式
        String imageStr= captcha.toBase64();

        //生成唯一字符串发给前端，当做是验证码的唯一标识(数据库表的主键) ，也可以叫令牌
        String uuid= IdUtil.fastUUID();
        //redis中以String类型来存放
        String redisKey=RedisConstans.getRedisKey(RedisConstans.CAPTCHA_KEY,uuid);
        //存放到redis中，过期时间5分钟
        this.redisTemplate.opsForValue().set(redisKey,value, RedisConstans.CAPTCHA_EXPIRED_MINUTE, TimeUnit.MINUTES);

        //输出结果
        Map<String,Object> result=new HashMap<>(8);
        result.put("uuid", uuid);
        result.put("image", imageStr);

        return R.okHasData(result);
    }




}
